package modelLayer;
import java.util.ArrayList;

public class PersonContainer
{
    private ArrayList<Person> personArray;
    private Person newPerson;
    private static PersonContainer instance;
    private PersonContainer()
    {
        personArray = new ArrayList<Person>();
    }
    public static PersonContainer getInstance()
    {
        if(instance == null)
        {
            instance = new PersonContainer();
        }
        return instance;
    }
    
    public void addPerson(Person person)
    {
        personArray.add(person);
    }
    
    public boolean findPerson(String searchTerm)
    {
        boolean isFound = false;
        int i = 0;
        for(Person person : personArray)
        {
            if(person.getPerson("name").toLowerCase().contains(searchTerm.toLowerCase()) || person.getPerson("phone").equals(searchTerm))
            {
                isFound = true;
                i++;
                System.out.println("Person " + i + ":");
                person.printPerson();
                System.out.println("--------------");
            }
        }
        return isFound;
    }
    
    public boolean createPerson(String name, String address, String postalCode, String city, String phone)
    {
        if(getPerson(name, address, postalCode, city, phone) == null)
        {
            if(getPerson(phone) == null)
            {
                System.out.print('\u000C');
                addPerson(newPerson = new Person(name, address, postalCode, city, phone));
                System.out.println("Person added");
                return true;
            }
            else
            {
                System.out.println("ERROR: Phone number already used!");
                return false;
            }
        }
        else
        {
            System.out.println("ERROR: Person already exists!");
            return false;
        }
    }
    
    public Person getPerson(String name, String address, String postalCode, String city, String phone)
    {
        for(Person person : personArray)
        {
            if(person.getPerson("name").equals(name) && person.getPerson("address").equals(address) && person.getPerson("postalCode").equals(postalCode) && person.getPerson("city").equals(city) && person.getPerson("phone").equals(phone))
            {
                return person;
            }
        }
        return null;
    }
    
    public Person getPerson(String searchTerm)
    {
        for(Person person : personArray)
        {
            if(person.getPerson("phone").equals(searchTerm))
            {
                return person;
            }
        }
        return null;
    }
    
    
    public boolean deletePerson(String searchTerm)
   {
       boolean wasRemoved = false;
       int i = 0;
       for(Person person : personArray)
        {
            if(person.getPerson("phone").equals(searchTerm))
            {
                wasRemoved = true;
                i = personArray.indexOf(person);
            }
        }
       if (wasRemoved)
       {
           personArray.remove(i);
       }
       return wasRemoved;
   }
   
   public boolean updatePerson(String searchTerm, String name, String address, String postalCode, String city, String phoneNumber)
   {
       Person updatingPerson = getPerson(searchTerm);
       boolean updated = false;
       if(updatingPerson != null)
       {
           updatingPerson.setAddress(address);
           updatingPerson.setCity(city);
           updatingPerson.setName(name);
           updatingPerson.setPhone(phoneNumber);
           updatingPerson.setPostalCode(postalCode);
           updated = true;
        }
        else
        {
            System.out.println("person not found");
        }
       return updated;
   }
}